package com.ruoyi.web.controller.construction;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.construction.RentalService;
import com.ruoyi.system.domain.construction.Team;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.SupplierInfo;
import com.ruoyi.system.service.construction.RentalServiceService;
import com.ruoyi.system.service.construction.TeamService;
import com.ruoyi.system.service.zhaungxiuservice.shopService.SupplierInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 租赁服务信息Controller
 *
 * @author zhaodejian
 * @date 2020-11-27
 */
@RestController
@RequestMapping("/construction/service")
public class RentalServiceController extends BaseController {

    @Autowired
    private RentalServiceService rentalServiceService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private SupplierInfoService supplierInfoService;
    @Autowired
    private TeamService teamService;
    /**
     * 查询租赁服务信息列表
     */
    @PreAuthorize("@ss.hasPermi('construction:service:list')")
    @GetMapping("/list")
    public TableDataInfo list(RentalService rentalService)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId1 = loginUser.getUser().getUserId();
        rentalService.setUserId(userId1);
        List<RentalService> list = rentalServiceService.selectRentalServiceList(rentalService);
        return getDataTable(list);
    }

    /**
     * 导出租赁服务信息列表
     */
    @PreAuthorize("@ss.hasPermi('construction:service:export')")
    @Log(title = "租赁服务信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RentalService rentalService)
    {
        List<RentalService> list = rentalServiceService.selectRentalServiceList(rentalService);
        ExcelUtil<RentalService> util = new ExcelUtil<RentalService>(RentalService.class);
        return util.exportExcel(list, "service");
    }

    /**
     * 获取租赁服务信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('construction:service:query')")
    @GetMapping(value = "/{rentalId}")
    public AjaxResult getInfo(@PathVariable("rentalId") Integer rentalId)
    {
        return AjaxResult.success(rentalServiceService.selectRentalServiceById(rentalId));
    }

    /**
     * 新增租赁服务信息
     */
    @PreAuthorize("@ss.hasPermi('construction:service:add')")
    @Log(title = "租赁服务信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RentalService rentalService)
    {
        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        for (int i=0;i<8;i++){
            stringBuilder.append(random.nextInt(10));
        }
        String num =stringBuilder.toString();
        rentalService.setRentalCore(num);
        rentalService.setModifiedTime(new Date());
        return toAjax(rentalServiceService.insertRentalService(rentalService));
    }

    /**
     * 修改租赁服务信息
     */
    @PreAuthorize("@ss.hasPermi('construction:service:edit')")
    @Log(title = "租赁服务信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RentalService rentalService)
    {
        return toAjax(rentalServiceService.updateRentalService(rentalService));
    }

    /**
     * 删除租赁服务信息
     */
    @PreAuthorize("@ss.hasPermi('construction:service:remove')")
    @Log(title = "租赁服务信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{rentalIds}")
    public AjaxResult remove(@PathVariable Integer[] rentalIds)
    {
        return toAjax(rentalServiceService.deleteRentalServiceByIds(rentalIds));
    }



    @PreAuthorize("@ss.hasPermi('construction:service:findAllSupper')")
    @GetMapping("/findAllSupper")
    public TableDataInfo findAllSupper(SupplierInfo supplierInfo){
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId1 = loginUser.getUser().getUserId();
        supplierInfo.setUserId(userId1);
        List<SupplierInfo> all = supplierInfoService.findAll(supplierInfo);
        return getDataTable(all);

    }


    @PreAuthorize("@ss.hasPermi('construction:service:findAllTeam')")
    @GetMapping("/findAllTeam")
    public  List<Team> findAllTeam(){
        return teamService.findAll();
    }

}
